package com.dycx.cms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.converter.ArticleContentConverter;
import com.dycx.cms.dto.ArticleContentDTO;
import com.dycx.cms.entity.ArticleContentDO;
import com.dycx.cms.mapper.ArticleContentMapper;
import com.dycx.cms.service.ArticleContentService;
import com.dycx.cms.vo.ArticleContentVO;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 文章详情(ArticleContent)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-25 08:59:02
 */

@Service
@AllArgsConstructor
public class ArticleContentServiceImpl extends ServiceImpl<ArticleContentMapper, ArticleContentDO> implements ArticleContentService {

    private ArticleContentMapper articleContentMapper;
    private final OssFileService ossFileService;


    /**
     * 新增数据
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean add(ArticleContentDTO dto) {
        dto.setContent(ossFileService.regexBase64Upload(dto.getContent()));
        ArticleContentDO entity = ArticleContentConverter.dtoToEntity(dto);
        if(!this.save(entity)) return false;
        ossFileService.setUse(entity.getContent(), true);
        return true;
    }

    /**
     * 修改数据
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(ArticleContentDTO dto) {
        dto.setContent(ossFileService.regexBase64Upload(dto.getContent()));

        LambdaQueryWrapper<ArticleContentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleContentDO::getArticleId, dto.getArticleId());

        ArticleContentDO articleContentDO = getOne(wrapper);
        ossFileService.setDeleted(articleContentDO.getContent(), true);

        ArticleContentDO entity = ArticleContentConverter.dtoToEntity(dto);
        if(!this.update(entity, wrapper)) return false;

        ossFileService.setUse(dto.getContent(), true);
        return true;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param articleId
     * @return
     */
    @Override
    public ArticleContentVO getInfo(Integer articleId) {
        LambdaQueryWrapper<ArticleContentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleContentDO::getArticleId, articleId);

        return ArticleContentConverter.entityToVo(this.getOne(wrapper));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return this.articleContentMapper.getMaxSort();
    }



}

